<template>
    <div class='approval'>
        <el-card class="box-card" style="margin-top: 110px;">
            <div class='title'>
                {{$t('lenders.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                    <!-- 业务ID -->
                    <div class='searchItem'>
                        <span>{{$t('lenders.orderId')}}</span>
                        <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                    </div>
                    <!-- 客户姓名 -->
                    <div class='searchItem'>
                        <span>{{$t('lenders.realName')}}</span>
                        <el-input v-model='listQuery.realName' :placeholder="$t('rule.realName')" size='small'></el-input>
                    </div>
                    <!-- 手机号 -->
                    <div class='searchItem'>
                        <span>{{$t('lenders.phoneNo')}}</span>
                        <el-input v-model='listQuery.phoneNo' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                    </div>
                    <!-- 任务状态 -->
                    <div class='searchItem'>
                        <span>{{$t('processing.taskStatus')}}</span>
                        <el-select v-model='listQuery.taskStatus' size='small'>
                            <el-option
                            v-for='item in taskStatus'
                            :key='item.value'
                            :label='item.label'
                            :value='item.value'>
                            </el-option>
                        </el-select>
                    </div>
                    <!-- 逾期天数 -->
                    <div class="searchItem searchItem-overdue">
                        <span>{{$t('processing.overDay')}}</span>
                        <el-input v-model='listQuery.overDayBegin' size='small'></el-input>
                        <b style="font-weight: normal; margin: 0 10px;font-size: 12px; color: #5f5f61;">{{$t('lenders.go')}}</b>
                        <el-input v-model='listQuery.overDayEnd' size='small'></el-input>
                        <b style="font-weight: normal; margin-left: 10px;font-size: 12px; color: #5f5f61;">{{$t('processing.day')}}</b>
                    </div>
                    <!-- 催收员 -->
                    <div class='searchItem'>
                        <span>{{$t('processing.collector')}}</span>
                        <el-input v-model='listQuery.collectUserName' :placeholder="$t('rule.collector')" size='small'></el-input>
                    </div>
                    <!-- 结清还款时间 -->
                    <div class="searchItem searchItem-date">
                        <span style="display: inline-block;width: 100px;">{{$t('dispatch.settleTime')}}</span>
                        <el-date-picker
                        size='small'
                        v-model="listQuery.beginSettleTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('rule.beginDate')">
                        </el-date-picker>
                        <b>{{$t('approval.go')}}</b>
                        <el-date-picker
                        size='small'
                          v-model="listQuery.endSettleTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :placeholder="$t('rule.endDate')">
                        </el-date-picker>
                    </div>
                    <!-- 申请时间 -->
                    <div class='searchItem searchItem-date'>
                        <span>{{$t('lenders.time')}}</span>
                        <el-date-picker
                        size='small'
                        class="dates"
                        v-model="listQuery.beginDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('rule.beginDate')">
                        </el-date-picker>
                        <b>{{$t('lenders.go')}}</b>
                        <el-date-picker
                        size='small'
                        class="dates"
                          v-model="listQuery.endDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :placeholder="$t('rule.endDate')">
                        </el-date-picker>
                    </div>
                    <!-- 领取时间 -->
                    <div class='searchItem searchItem-date'>
                        <span>{{$t('processing.taskDate')}}</span>
                        <el-date-picker
                        size='small'
                        class="dates"
                        v-model="listQuery.crtTimeB"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('rule.beginDate')">
                        </el-date-picker>
                        <b>{{$t('lenders.go')}}</b>
                        <el-date-picker
                        size='small'
                        class="dates"
                          v-model="listQuery.crtTimeE"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :placeholder="$t('rule.endDate')">
                        </el-date-picker>
                    </div>
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
            <div class='title'>
                {{$t('processing.taskList')}}
            </div>
            <div class='menu-list'>
                <!-- <button type='button' :disabled='isDisable2' @click='approvalFn'>{{$t('processing.coll')}}</button> -->
                <!-- 领取任务 -->
                <button type='button' @click='receiveFn'>{{$t('processing.receiveTask')}}</button>
                <!-- 任务退回 -->
                <button type='button' @click='returnFn'>{{$t('processing.returnTask')}}</button>
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 95%'
                class='tables'
                @selection-change='handleSelectionChange'
                border
                stripe
                size='mini'>
                <el-table-column
                header-align='center'
                align='center'
                type='selection'
                width='55'>
                </el-table-column>
                <!-- 业务ID -->
                <el-table-column
                :label="$t('lenders.orderId')" align="center" header-align="center" 
                width='140'
                fixed="left">
                <template slot-scope="scope">
                    <span class="href" @click="hrefFn(scope.row)">{{scope.row.orderId}}</span>
                </template>
                </el-table-column>
                <!-- 客户姓名 -->
                <el-table-column
                prop='realName' align="center" header-align="center" 
                :label="$t('lenders.realName')"
                width='180'>
                </el-table-column>
                <!-- 手机号 -->
                <el-table-column
                prop='appUserName' align="center" header-align="center" 
                :label="$t('lenders.phoneNo')"
                width='120'>
                </el-table-column>
                <!-- 产品名称 -->
                <el-table-column
                prop='proName' align="center" header-align="center" 
                :label="$t('processing.proName')"
                width='90'>
                </el-table-column>
                <!-- 催收员 -->
                <el-table-column
                prop='collectUserName' align="center" header-align="center" 
                :label="$t('processing.collector')"
                width='90'>
                </el-table-column>
                <!-- 领取时间 -->
                <el-table-column
                prop='crtTime' align="center" header-align="center" 
                :label="$t('processing.taskDate')"
                width='150'>
                </el-table-column>
                <!-- 金额 -->
                <el-table-column
                prop='approvedAmt' align="center" header-align="center" 
                :label="$t('processing.appAmt')"
                width='90'>
                </el-table-column>
                <!-- 实收金额 -->
                <el-table-column
                prop='realRepayAmt' align="center" header-align="center" 
                :label="$t('paymentHistory.amount')"
                width='90'>
                </el-table-column>
                <!-- 最新一次还款金额 -->
                <el-table-column
                header-align='center'
                align='center'
                :label="$t('processing.latestRepayAmount')"
                width='150'>
                <template slot-scope="scope">
                    <span class="hit" v-if="scope.row.latestRepaymentAmount === ''">
                        <a href="javascript:void(0)" @click="lookUp(scope.row)">{{$t('approvalDetail.hitsee')}}</a>
                    </span>
                    <span v-else>
                       {{scope.row.latestRepaymentAmount}}
                    </span>
                </template> 
                </el-table-column>
                <!-- 申请时间 -->
                <el-table-column
                prop="orderCrtTime" align="center" header-align="center" 
                :label="$t('processing.orderCrtTime')"
                width='150'>
                </el-table-column>
                <!-- 到期时间 -->
                <el-table-column
                prop="lastPayDate" align="center" header-align="center" 
                :label="$t('processing.lastPayDate')"
                width='150'>
                </el-table-column>
                <!-- 首次到期还款时间 -->
                <el-table-column
                prop="firstDueDate" align="center" header-align="center" 
                :label="$t('processing.firstRepayTime')"
                width='150'>
                </el-table-column>
                <!-- 逾期天数 -->
                <el-table-column
                prop="overdueDay" align="center" header-align="center" 
                :label="$t('processing.overdueDay')"
                width='90'>
                </el-table-column>
                <!-- 历史逾期天数 -->
                <el-table-column
                prop="overdueDayNew" align="center" header-align="center" 
                :label="$t('processing.hisOverdueDay')"
                width='120'>
                </el-table-column>
                <!-- 滞纳金 -->
                <el-table-column
                prop='overdueAmt' align="center" header-align="center" 
                :label="$t('processing.overdueAmt')"
                width='150'>
                </el-table-column>
                <!-- 逾期利息 -->
                <el-table-column
                prop='overdueInterest' align="center" header-align="center" 
                :label="$t('processing.overdueInterest')"
                width='150'>
                </el-table-column>
                <!-- <el-table-column
                prop='extensionTime'
                :label="$t('dispatch.extensionTime')"
                width='112'>
                </el-table-column> -->
                <!-- 结清还款时间 -->
                <el-table-column
                prop='settleTime' align="center" header-align="center" 
                :label="$t('dispatch.settleTime')"
                width='112'>
                </el-table-column>
                <!-- 通话结果 -->
                <el-table-column
                prop='callResult' align="center" header-align="center" 
                :label="$t('processing.callResult')"
                width='90'>
                <template slot-scope="scope">
                    <span v-if="scope.row.callResult=== '1'">{{$t('processing.normal')}}</span>
                    <span v-if="scope.row.callResult=== '2'">{{$t('processing.fail')}}</span>
                </template>
                </el-table-column>
                <!-- 失败原因 -->
                <el-table-column
                prop='callFailedReason' align="center" header-align="center" 
                :label="$t('processing.callFailedReason')"
                width='180'>
                <template slot-scope="scope">
                    <span v-if="scope.row.callFailedReason=== '1'">{{$t('processing.reason1')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '2'">{{$t('processing.reason2')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '3'">{{$t('processing.reason3')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '4'">{{$t('processing.reason4')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '5'">{{$t('processing.reason5')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '6'">{{$t('processing.reason6')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '7'">{{$t('processing.reason7')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '8'">{{$t('processing.reason8')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '9'">{{$t('processing.reason9')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '10'">{{$t('processing.reason10')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '11'">{{$t('processing.reason11')}}</span>
                    <span v-else-if="scope.row.callFailedReason=== '12'">{{$t('processing.reason12')}}</span>
                </template>
                </el-table-column>
                <!-- 处理结果 -->
                <el-table-column
                prop='dealResult' align="center" header-align="center" 
                :label="$t('processing.dealResult')"
                width='90'>
                <template slot-scope="scope">
                    <span v-if="scope.row.dealResult=== '1'">{{$t('processing.result1')}}</span>
                    <span v-else-if="scope.row.dealResult=== '2'">{{$t('processing.result2')}}</span>
                    <span v-else-if="scope.row.dealResult=== '3'">{{$t('processing.result3')}}</span>
                    <span v-else-if="scope.row.dealResult=== '4'">{{$t('processing.result4')}}</span>
                    <span v-else-if="scope.row.dealResult=== '5'">{{$t('processing.result5')}}</span>
                    <span v-else-if="scope.row.dealResult=== '6'">{{$t('processing.result6')}}</span>
                    <span v-else-if="scope.row.dealResult=== '7'">{{$t('processing.result7')}}</span>
                    <span v-else-if="scope.row.dealResult=== '8'">{{$t('processing.result8')}}</span>
                    <span v-else-if="scope.row.dealResult=== '9'">{{$t('processing.result9')}}</span>
                    <span v-else-if="scope.row.dealResult=== '10'">{{$t('processing.result10')}}</span>
                    <span v-else-if="scope.row.dealResult=== '11'">{{$t('processing.result11')}}</span>
                </template>
                </el-table-column>
                <!-- 催收意见 -->
                <el-table-column
                prop='collectRemark' align="center" header-align="center" 
                :label="$t('processing.collectRemark')"
                width='180'
                show-overflow-tooltip>
                </el-table-column>
                <!-- 任务状态 -->
                <el-table-column
                prop='status' align="center" header-align="center" 
                :label="$t('processing.taskStatus')"
                width='90'>
                <template slot-scope="scope">
                    <span v-if="scope.row.status=== 10">{{$t('processing.status1')}}</span>
                    <span v-else-if="scope.row.status === 50">{{$t('processing.status1')}}</span>
                    <span v-else>{{$t('processing.status2')}}</span>
                </template>
                </el-table-column>
                <!-- 业务渠道 -->
                <el-table-column
                prop='appShortName' align="center" header-align="center" 
                :label="$t('processing.appShortName')"
                width='90'>
                </el-table-column>
                <el-table-column
                header-align="center"
                align="center"
                :label="$t('user.operation')"
                fixed="right">
                    <template slot-scope="scope">
                        <!-- 催收 -->
                        <el-button
                        type="primary"
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">{{$t('processing.coll')}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="listQuery.page"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="listQuery.limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { page, getTask, returnTask, latestAmount } from 'api/collection/processing/index'
export default {
    data() {
        return { 
        listLoading: false,
        listQuery: {
            limit: 5,//每页条数
            page: 1,//当前页数
            orderId: "",//订单号
            realName: "",//姓名
            phoneNo: "",//手机
            beginDate: "",//申请时间
            endDate: "",
            taskStatus: 1,
            overDayBegin: "",//逾期天数
            overDayEnd: "",
            collectUserName: "",//催收员
            crtTimeB: "", //催收时间
            crtTimeE: "",
            beginSettleTime: "", //结清还款时间
            endSettleTime: ""
        },
        total: null,
        tableData: [],
        multipleSelection: [],
        isDisable2: true,
        lanStatus: null,
        taskStatus: [
            {
                value: '',
                label: this.$t('rule.choose')
            },
            {
                value: 1,
                label: this.$t('processing.status1')
            },
            {
                value: 2,
                label: this.$t('processing.status2')
            }
        ]
        };
    },
    created() {
        if (this.language === 'zh') {
            this.lanStatus = 1;
        } else if (this.language === 'en') {
            this.lanStatus = 2;
        } else {
            this.lanStatus = 3;
        }
        if (this.processPage.has(this.$route.path)) {
            this.listQuery = this.processPage.get(this.$route.path)
        }
        this.getList();
    },
    computed: {
        ...mapGetters(["elements", "language", "processPage"])
    },
    methods: {
        //查看最新一次还款金额
        lookUp(row) {
            latestAmount(row.orderId)
            .then(res => {
                this.tableData.forEach(item => {
                    if (row.orderId === item.orderId) {
                        let re = /(?=(?!\b)(\d{3})+$)/g;
                        let str = String(res.data)
                        res.data = str.replace(re, ',');
                        item.latestRepaymentAmount = res.data
                    }
                })
            })
        },
        //点击催收
        handleEdit(index, row) {
            this.$store.dispatch('setCollDetail', row)
            this.$router.push({
                path: '/collection/collDetail', 
                name: '催收详情'
            })  
        },
        hrefFn(row) {
            this.$store.dispatch('setCollDetail', row)
            this.$router.push({
                path: '/collection/collDetail', 
                name: '催收详情'
            })  
        },
        getList() {
            this.listLoading = true;
            this.$store.dispatch('saveProcessPage', { path: this.$route.path, listQuery: this.listQuery });
            if (this.tableData.length > 0) {
                this.isDisable2 = false;
            } else {
                this.isDisable2 = true;
            }
            page(this.listQuery)
            .then(response => {
                response.data.rows.forEach(item => {
                    let re = /(?=(?!\b)(\d{3})+$)/g;
                    let str = String(item.approvedAmt)
                    item.approvedAmt = str.replace(re, ',');
                    let str2 = String(item.overdueAmt)
                    item.overdueAmt = str2.replace(re, ',');
                    let str3= String(item.overdueInterest)
                    item.overdueInterest = str3.replace(re, ',');
                    let str4 = String(item.realRepayAmt)
                    item.realRepayAmt = str4.replace(re, ',');
                })
                if (response.status === 200) {
                    this.listLoading = false;
                    this.tableData = response.data.rows;
                    this.total = Number(response.data.total);
                }
            })
        },
        //查询
        search() {
            this.listQuery.page = 1;
            this.getList();
        },
        //重置
        reset() {
            this.listQuery.limit = 5;
            this.listQuery.page = 1;
            this.listQuery.orderId = '';
            this.listQuery.realName = '';
            this.listQuery.phoneNo = '';
            this.listQuery.beginDate = '';
            this.listQuery.endDate = '';
            this.listQuery.taskStatus = 1;
            this.listQuery.overDayBegin = '';
            this.listQuery.overDayEnd = '';
            this.listQuery.crtTimeB = '';
            this.listQuery.crtTimeE = '';
            this.listQuery.beginSettleTime = "";
            this.listQuery.endSettleTime = ""
        },
        //选中的数据
        handleSelectionChange(val) {
            this.multipleSelection = val;
            if (this.multipleSelection.length !== 0) {
                if (this.multipleSelection[0].status === 20 || this.multipleSelection[0].status === 100) {
                    this.isDisable2 = true;//禁用
                } else {
                    this.isDisable2 = false;//不禁用
                }
            }
        },
        //领取任务
        receiveFn() {
            getTask()
            .then(response => {
                if(response.status === 200) {
                    this.$message({
                        showClose: true,
                        message: response.message,
                        type: "success"
                    });
                    this.getList()
                    if (this.tableData.length > 0) {
                        this.isDisable2 = false;
                    }
                } else {
                    this.$message({
                        showClose: true,
                        message: response.message,
                        type: "warning"
                    });
                }
            })
        },
        //任务退回
        returnFn() {
            if(this.multipleSelection.length > 0) { 
                let arr = [];
                this.multipleSelection.forEach(item => {
                    arr.push(Number(item.id))
                })
                returnTask(arr)
                .then(response => {
                    if(response.status === 200) {
                        this.$message({
                            showClose: true,
                            message: this.$t('processing.returnsuc'),
                            type: 'success'
                        });
                        this.getList()
                    }
                })
            } else {
                this.$message({
                    showClose: true,
                    message: this.$t('lenders.msg1'),
                    type: 'warning'
                });
            }
        },
        handleSizeChange(val) {
            this.listQuery.limit = val;
            this.getList()
        },
        handleCurrentChange(val) {
            this.listQuery.page = val;
            this.getList()
        },
    }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div{
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
    .searchItem-overdue {
        display: flex;
        align-items: center;
        padding: 10px  0;
        div {
            width: 48px;
        }
    }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}

.href {
  border-bottom: 1px solid #000;
  cursor: pointer; 
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
.hit {
    &>a {
        color:#20a0ff;
        text-decoration: underline;
    }
}
</style>